﻿'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class IcqPrivacyService
    Inherits ContextService
    Implements Interfaces.IPrivacyService

    Private ReadOnly _IgnoreList As Core.Interfaces.IReadOnlyNotifyingCollection(Of Interfaces.IContact)
    Private ReadOnly _InvisibleList As Core.Interfaces.IReadOnlyNotifyingCollection(Of Interfaces.IContact)
    Private ReadOnly _VisibleList As Core.Interfaces.IReadOnlyNotifyingCollection(Of Interfaces.IContact)

    Public ReadOnly Property IgnoreList() As Core.Interfaces.IReadOnlyNotifyingCollection(Of Interfaces.IContact) Implements Interfaces.IPrivacyService.IgnoreList
        Get
            Return _IgnoreList
        End Get
    End Property

    Public ReadOnly Property InvisibleList() As Core.Interfaces.IReadOnlyNotifyingCollection(Of Interfaces.IContact) Implements Interfaces.IPrivacyService.InvisibleList
        Get
            Return _InvisibleList
        End Get
    End Property

    Public ReadOnly Property VisibleList() As Core.Interfaces.IReadOnlyNotifyingCollection(Of Interfaces.IContact) Implements Interfaces.IPrivacyService.VisibleList
        Get
            Return _VisibleList
        End Get
    End Property

    Public Sub New(ByVal context As Interfaces.IContext)
        MyBase.New(context)

        Dim connector As IcqConnector = TryCast(context.GetService(Of Interfaces.IConnector)(), IcqConnector)

        If connector Is Nothing Then Throw New InvalidCastException("Context Connector Service must be of Type IcqConnector")

        Dim svStorage As IcqStorageService = TryCast(context.GetService(Of Interfaces.IStorageService)(), IcqStorageService)

        _IgnoreList = svStorage.IgnoreList
        _InvisibleList = svStorage.InvisibleList
        _VisibleList = svStorage.VisibleList
    End Sub

    Public Sub AddContactToIgnoreList(ByVal contact As Interfaces.IContact) Implements Interfaces.IPrivacyService.AddContactToIgnoreList
        Dim trans As AddContactToIgnoreListTransaction
        Dim svStorage As IcqStorageService
        Dim icontact As IcqContact

        svStorage = DirectCast(Context.GetService(Of Interfaces.IStorageService)(), IcqStorageService)
        icontact = DirectCast(contact, IcqContact)

        trans = New AddContactToIgnoreListTransaction(svStorage, icontact)

        svStorage.CommitSSITransaction(trans)
    End Sub

    Public Sub AddContactToInvisibleList(ByVal contact As Interfaces.IContact) Implements Interfaces.IPrivacyService.AddContactToInvisibleList
        Dim trans As AddContactToInvisibleListTransaction
        Dim svStorage As IcqStorageService
        Dim icontact As IcqContact

        svStorage = DirectCast(Context.GetService(Of Interfaces.IStorageService)(), IcqStorageService)
        icontact = DirectCast(contact, IcqContact)

        trans = New AddContactToInvisibleListTransaction(svStorage, icontact)

        svStorage.CommitSSITransaction(trans)
    End Sub

    Public Sub AddContactToVisibleList(ByVal contact As Interfaces.IContact) Implements Interfaces.IPrivacyService.AddContactToVisibleList
        Dim trans As AddContactToVisibleListTransaction
        Dim svStorage As IcqStorageService
        Dim icontact As IcqContact

        svStorage = DirectCast(Context.GetService(Of Interfaces.IStorageService)(), IcqStorageService)
        icontact = DirectCast(contact, IcqContact)

        trans = New AddContactToVisibleListTransaction(svStorage, icontact)

        svStorage.CommitSSITransaction(trans)
    End Sub

    Public Sub RemoveContactFromIgnoreList(ByVal contact As Interfaces.IContact) Implements Interfaces.IPrivacyService.RemoveContactFromIgnoreList
        Dim trans As RemoveContactFromIgnoreListTransaction
        Dim svStorage As IcqStorageService
        Dim icontact As IcqContact

        svStorage = DirectCast(Context.GetService(Of Interfaces.IStorageService)(), IcqStorageService)
        icontact = DirectCast(contact, IcqContact)

        trans = New RemoveContactFromIgnoreListTransaction(svStorage, icontact)

        svStorage.CommitSSITransaction(trans)
    End Sub

    Public Sub RemoveContactFromInvisibleList(ByVal contact As Interfaces.IContact) Implements Interfaces.IPrivacyService.RemoveContactFromInvisibleList
        Dim trans As RemoveContactFromInvisibleListTransaction
        Dim svStorage As IcqStorageService
        Dim icontact As IcqContact

        svStorage = DirectCast(Context.GetService(Of Interfaces.IStorageService)(), IcqStorageService)
        icontact = DirectCast(contact, IcqContact)

        trans = New RemoveContactFromInvisibleListTransaction(svStorage, icontact)

        svStorage.CommitSSITransaction(trans)
    End Sub

    Public Sub RemoveContactFromVisibleList(ByVal contact As Interfaces.IContact) Implements Interfaces.IPrivacyService.RemoveContactFromVisibleList
        Dim trans As RemoveContactFromVisibleListTransaction
        Dim svStorage As IcqStorageService
        Dim icontact As IcqContact

        svStorage = DirectCast(Context.GetService(Of Interfaces.IStorageService)(), IcqStorageService)
        icontact = DirectCast(contact, IcqContact)

        trans = New RemoveContactFromVisibleListTransaction(svStorage, icontact)

        svStorage.CommitSSITransaction(trans)
    End Sub

End Class
